package com.k.kc.util.tree.demo2;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class Test {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    public static void main(String[] args) throws JsonProcessingException {
        List<FunctionTreeDTO> listZ = new ArrayList<>();
        List<FunctionTreeDTO> rootNodeList = getRootData(3);
        // 101 -level1
        List<FunctionTreeDTO> childrenNodeList = getChildData("101", 4);

        List<FunctionTreeDTO> childrenNodeList3 = getChildData3("1000",3);

        listZ.addAll(rootNodeList);
        listZ.addAll(childrenNodeList);
        listZ.addAll(childrenNodeList3);

        long start = System.currentTimeMillis();
        TreeUtil<FunctionTreeDTO> treeUtil = new TreeUtil(listZ);
        List<FunctionTreeDTO> z = treeUtil.build();
        long end = System.currentTimeMillis();
        System.out.println("MY:"+(end-start));
        System.out.println(MAPPER.writeValueAsString(z));

//        long s2 = System.currentTimeMillis();
//        List<FunctionTreeDTO> z2 = TreeBuildUtil.buildTreeDTO(listZ,new ArrayList<>());
//        long e2 = System.currentTimeMillis();
//        System.out.println("OTHER:"+(e2-s2));
//
//
//        long s3 = System.currentTimeMillis();
//        List<FunctionTreeDTO> z3 = TreeBuildUtil.buildTreeDTO(listZ,new ArrayList<>());
//        long e3 = System.currentTimeMillis();
//        System.out.println("OTHER3:"+(e3-s3));


        long start4 = System.currentTimeMillis();
        TreeUtil<FunctionTreeDTO> treeUtil4 = new TreeUtil(listZ);
        List<FunctionTreeDTO> z4 = treeUtil4.build();
        long end4 = System.currentTimeMillis();
        System.out.println("MY4:"+(end4-start4));


    }


    private static List<FunctionTreeDTO> getRootData(int count) {
        List<FunctionTreeDTO> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            FunctionTreeDTO d = new FunctionTreeDTO();
            d.setCode((100 + i) + "");
            d.setParentCode("1");
            d.setFunctionName("root-name" + i);

            d.setKey(d.getCode());
            d.setParentKey(d.getParentCode());
            d.setSort(i);
            list.add(d);
        }
        return list;
    }

    private static List<FunctionTreeDTO> getChildData(String pKey, int count) {
        List<FunctionTreeDTO> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            FunctionTreeDTO d = new FunctionTreeDTO();

            d.setCode((1000 + i) + "");
            d.setParentCode(pKey);
            d.setFunctionName("child-name" + i);

            d.setKey(d.getCode());
            d.setParentKey(d.getParentCode());
            d.setSort(i);
            list.add(d);
        }
        return list;
    }
    private static List<FunctionTreeDTO> getChildData3(String pKey, int count) {
        List<FunctionTreeDTO> list = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            FunctionTreeDTO d = new FunctionTreeDTO();

            d.setCode((2000 + i) + "");
            d.setParentCode(pKey);
            d.setFunctionName("child-name" + i);

            d.setKey(d.getCode());
            d.setParentKey(d.getParentCode());
            d.setSort(i);
            list.add(d);
        }
        return list;
    }
}
